<script>
	import pako from './components/pako.min.js';
	let socketDeep;
	export default {
		onLaunch: function() {
      uni.removeStorageSync('tabbar_list');
		},
		onShow: function() {
			this.$http('api/newapi/yinCang').then(res => {
				uni.setTabBarItem({
				  index: 1,
					visible:res == 0 ? false : true
				})
			})
			// #ifdef APP-PLUS
			this.checkUpdate()
			// #endif
		},
		methods: {
			checkUpdate() {
				// 更新版本接口
				var that = this;
				// 获取manifest.json 的信息
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					let curVersion = wgtinfo.versionCode;
					uni.request({
						url:this.$url + 'api/neword/banben',
						success: (data) => {
							let res = data.data.data
							if (plus.os.name.toLowerCase() == 'ios') {
								if (res.ios > curVersion)
									// 跳转到下载页面 
									uni.showModal({
										showCancel:res.status ==1 ? false : true,
										title: '版本更新',
										confirmText: '更新',
										success: (e) => {
											if (e.confirm) {
												plus.runtime.openURL(res.ios_x);
											}
										}
									});
							} else {
								if (res.android > curVersion) {
									uni.showModal({
										showCancel: res.status == 1 ? false : true,
										title: '版本更新',
										confirmText: '更新',
										success: (e) => {
											if (e.confirm) {
												that.createDownload(res.android_x);
											}
										}
									});
								}
							}
						}
					});
				});
			},
			createDownload(url) {
				var dtask = plus.downloader.createDownload(url, {}, function(d, status) {
					uni.showToast({
						title: '下载完成',
						mask: false,
						duration: 1000
					});
					// 下载完成
					if (status == 200) {
						plus.runtime.install(
							plus.io.convertLocalFileSystemURL(d.filename), {},
							function(success) {
								uni.showToast({
									title: '安装成功',
									mask: false,
									duration: 1500
								});
							},
							function(error) {
								uni.showToast({
									title: '安装失败-01',
									mask: false,
									duration: 1500
								});
							}
						);
					} else {
						uni.showToast({
							title: '更新失败-02',
							mask: false,
							duration: 1500
						});
					}
				});
				try {
					dtask.start(); // 开启下载的任务
					var prg = 0;
					var showLoading = plus.nativeUI.showWaiting('正在下载'); //创建一个showWaiting对象
					dtask.addEventListener('statechanged', function(task, status) {
						// 给下载任务设置一个监听 并根据状态  做操作
						switch (task.state) {
							case 1:
								showLoading.setTitle('正在下载');
								break;
							case 2:
								showLoading.setTitle('已连接到服务器');
								break;
							case 3:
								prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) *
									100);
								showLoading.setTitle('  正在下载' + prg + '%  ');
								break;
							case 4:
								plus.nativeUI.closeWaiting();
								//下载完成
								break;
						}
					});
				} catch (err) {
					plus.nativeUI.closeWaiting();
					uni.showToast({
						title: '更新失败-03',
						mask: false,
						duration: 1500
					});
				}
			},
		}
	}
</script>


<style lang="scss">
	:root {
		--window-bottom: calc(44px + env(safe-area-inset-bottom)) !important;
	}
	.uni-app--showtabbar uni-page-wrapper::after{
		height: calc(44px + env(safe-area-inset-bottom)) !important;
	}
	// #ifdef H5
	.uni-tabbar-bottom {
	  display: none;
	}
	// #endif


	uni-page-body {
		color: $gray !important;
		font-size: 30rpx !important;
	}

	// 自定义头部，状态栏盒子样式
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	// tab切换选中加下边框
	.active-bb {
		&::after {
			content: '';
			position: absolute;
			width: 90%;
			height: 5rpx;
			background-color: $glod;
			left: 5%;
			bottom: -25rpx;
		}
	}

	// filed
	.u-field {
		color: #fff;
		padding: 26rpx 34rpx !important;

		.u-label-text {
			font-weight: 700 !important;
		}
	}

	// 提交按钮
	// <u-button type="primary" @click="pay">确定</u-button>
	.u-btn--primary {
		background: #EFAA1C !important;
		border-radius: 46rpx;
		height: 92rpx;
		width: 650rpx;
		color: #262729 !important;
		font-size: 30rpx;
	}

	.u-btn--primary--disabled {
		background: #DEE4E8 !important;
		background-size: 100% 100%;
	}

	.u-size-default {
		height: 88rpx !important;
	}

	// cell组件右边箭头对其文字
	.u-cell_title {
		// font-weight: 700 !important;
		color: #fff !important;
	}

	.u-cell__right-icon-wrap {
		height: 55rpx !important;
	}

	// 九宫格
	.u-grid-item-box {
		padding: 30rpx 0 0 !important;
	}

	.text-ellipsis {
		display: block;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.text-ellipsis-2 {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.timeImg {
		width: 30rpx;
		height: 30rpx;
	}

	.collectPreImg {
		width: 684rpx;
		display: block;
		margin: 0 auto;
		height: 646rpx;
	}
	.preImg {
		width: 40rpx;
		height: 40rpx;
		position: absolute;
		z-index: 10;
		top: 45%;
		left: 50%;
		transform: translateX(-50%);
	}
	/*每个页面公共css */
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
	@import './common/common.scss'
</style>
